#ifndef _LOG_H_
#define _LOG_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#ifdef POSIX_PTHREAD
	#include <pthread.h>
#endif

#include <iostream>
#include <fstream>
#include <string>
#include "singleton.h"

using std::string;

enum LEVEL {
    LEVEL_PANIC = 0,
    LEVEL_FATAL,
    LEVEL_ERROR,
    LEVEL_WARN,
    LEVEL_INFO,
    LEVEL_DEBUG,
};

enum WRITE_DB {
	WRITE_DB_OFF = 0,
	WRITE_DB_ON,
};

enum ROTATE {
	ROTATE_OFF = 0,
	ROTATE_ON,
};
class GameLog {
public:
	GameLog();
	virtual ~GameLog();
	void start(int server_id, int level, int write_db, int rotate, int64_t max_size, int max_file);
	void stop();
	void panic(const char *fmt, ...);
	void fatal(const char *fmt, ...);
	void error(const char *fmt, ...);
	void warn(const char *fmt, ...);
	void info(const char *fmt, ...);
	void debug(const char *fmt, ...);

private:
	string file_name();
	void output(char *buf, int cnt);
	void rotate();
	void rotate_hour();

private:
	string				_log_file;
	int					_level;
	int					_fd;
	int					_write_db;
	int					_rotate;
	int64_t				_max_size;
	int64_t				_current_size;
	int					_max_file;
	string				_hour_str;
	int					_server_id;
#ifdef POSIX_PTHREAD
	pthread_mutex_t		_mutex;
#endif
};

#define GAME_LOG (*Singleton<GameLog>::Instance())

#endif
